package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.model.TbSpecification;
import com.dongdongshop.model.TbSpecificationExample;
import com.dongdongshop.model.TbSpecificationOption;
import com.dongdongshop.model.TbSpecificationOptionExample;
import com.dongdongshop.page.PageParms;
import com.dongdongshop.page.PageUtil;
import com.dongdongshop.service.SpecificationService;
import com.dongdongshop.vo.SpecificationOptionVO;
import com.dongdongshop.vo.SpecificationVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SpecificationServiceImpl implements SpecificationService {
    @Resource
    private TbSpecificationMapper specificationMapper;
    @Resource
    private TbSpecificationOptionMapper specificationOptionMapper;

    @Override
    public PageUtil getSpecifigcationList(PageParms<SpecificationVO> page) {
        SpecificationVO parms = page.getParms();
        Page<Object> objects = PageHelper.startPage(page.getPageNumber(), page.getPageSize());
        TbSpecificationExample specificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        if (StringUtils.isNotBlank(parms.getSpecName())) {
            criteria.andSpecNameLike("%" + parms.getSpecName() + "%");
        }
        List<TbSpecification> specificationList = specificationMapper.selectByExample(specificationExample);
        List<SpecificationVO> voList = specificationList.stream().map(specif -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(specif, vo);
            return vo;
        }).collect(Collectors.toList());
        PageInfo pageInfo = new PageInfo(voList);
        long total = objects.getTotal();
        pageInfo.setTotal(total);
        PageUtil pageUtil = new PageUtil().setTotal(pageInfo.getTotal()).setPageNumber(page.getPageNumber()).setPageSize(page.getPageSize()).setData(pageInfo.getList());
        pageUtil.setTotalPage(pageUtil.getTotal() % pageUtil.getPageSize() == 0 ? pageUtil.getTotal() / pageUtil.getPageSize() : (pageUtil.getTotal() / pageUtil.getPageSize()) + 1 );
        return pageUtil;

    }

    @Override
    public void addSpecifigcation(SpecificationVO specificationVO) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specificationVO, tbSpecification);
        try {
            //先添加specificationVO 返回id值
            int i = specificationMapper.insertSelective(tbSpecification);
            //再添加specificationOptionMapper;
            List<SpecificationOptionVO> list = specificationVO.getList();
            List<TbSpecificationOption> collect = list.stream().map(specificationOptionVO -> {
                TbSpecificationOption option = new TbSpecificationOption();
                BeanUtils.copyProperties(specificationOptionVO, option);
                return option;
            }).collect(Collectors.toList());
            specificationOptionMapper.insertOption(collect, tbSpecification.getId());
        } catch (Exception e) {
            throw e;
        }

    }

    @Override
    public SpecificationVO getSpecifigcationById(Long id) {
        TbSpecification tbSpecification = specificationMapper.selectByPrimaryKey(id);
        if (tbSpecification == null){
            return null;
        }
        SpecificationVO specificationVO = new SpecificationVO();
        BeanUtils.copyProperties(tbSpecification, specificationVO);
        return specificationVO;
    }


    @Override
    public void deleteBatch(List<Long> ids) {
        TbSpecificationExample tbSpecificationExample = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria = tbSpecificationExample.createCriteria();
        criteria.andIdIn(ids);
        TbSpecificationOptionExample tbSpecificationOptionExample = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria1 = tbSpecificationOptionExample.createCriteria();
        criteria1.andSpecIdIn(ids);
        try {
            //先删除规格表
            specificationMapper.deleteByExample(tbSpecificationExample);
            //再删除选项表
            specificationOptionMapper.deleteByExample(tbSpecificationOptionExample);
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public void updatedSpecifigcation(SpecificationVO specificationVO) {
        TbSpecification tbSpecification = new TbSpecification();
        BeanUtils.copyProperties(specificationVO, tbSpecification);
        try {
            //先修改specification
            int i = specificationMapper.updateByPrimaryKeySelective(tbSpecification);
            //先删除再添加specificationOptionMapper值
            TbSpecificationOptionExample optionExample = new TbSpecificationOptionExample();
            TbSpecificationOptionExample.Criteria criteria = optionExample.createCriteria();
            criteria.andSpecIdEqualTo(specificationVO.getId());
            specificationOptionMapper.deleteByExample(optionExample);
            //再添加specificationOptionMapper;
            List<SpecificationOptionVO> list = specificationVO.getList();
            List<TbSpecificationOption> collect = list.stream().map(speco -> {
                TbSpecificationOption tb = new TbSpecificationOption();
                BeanUtils.copyProperties(speco, tb);
                return tb;
            }).collect(Collectors.toList());
            specificationOptionMapper.insertOption(collect, specificationVO.getId());
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    public List<SpecificationVO> getAllSpecification() {
        List<TbSpecification> specificationList = specificationMapper.selectByExample(null);
        List<SpecificationVO> specificationVOList = specificationList.stream().map(spec -> {
            SpecificationVO vo = new SpecificationVO();
            BeanUtils.copyProperties(spec, vo);
            return vo;
        }).collect(Collectors.toList());
        return specificationVOList;
    }
}
